package com.leaves.leetcode;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public abstract class UnitTesting<K, V> {

    protected List<TestData> testDataList;


    protected UnitTesting() {
        testDataList = new ArrayList<>();
        generateTestData();
    }

    protected abstract void generateTestData();

    public abstract V run(K inputData);

    public List<V> executeTestCases() {
        List<V> res = new ArrayList<>();
        for (TestData testData : testDataList) {
            res.add(this.run(testData.inputData));
        }
        return res;
    }

    public List<Boolean> validate() {
        List<Boolean> res = new ArrayList<>();
        for (TestData testData : testDataList) {
//            res.add(testData.outputData.equals(this.run(testData.inputData)));
            String outputJsonStand = JSON.toJSONString(testData.outputData);
            String outputJson = JSON.toJSONString(this.run(testData.inputData));
            res.add(StringUtils.equals(outputJson, outputJsonStand));
        }
        return res;
    };

    protected TestData createTestData(K inputData, V outputData) {
        return new TestData(inputData, outputData);
    }

    @AllArgsConstructor
    protected class TestData {
        private K inputData;
        private V outputData;
    }
}
